package com.hzlj.position.locate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.exception.PubException;
import com.fz.common.dict.annotation.DictConvert;
import com.hzlj.position.config.common.enums.WarningType;
import com.hzlj.position.locate.common.dto.position.CrossAbnResultDTO;
import com.hzlj.position.locate.common.dto.position.PositionExtDTO;
import com.hzlj.position.locate.common.dto.position.PositionQueryParamDTO;
import com.hzlj.position.locate.common.dto.positionSkynet.PositionSkynetAddDTO;
import com.hzlj.position.locate.common.dto.positionSkynet.PositionSkynetQueryResultDTO;
import com.hzlj.position.locate.common.model.PositionSkynet;
import com.hzlj.position.locate.converter.PositionSkynetConverter;
import com.hzlj.position.locate.dao.PositionSkynetDao;
import com.hzlj.position.locate.service.PositionAddTaskService;
import com.hzlj.position.locate.service.PositionSkynetService;
import com.hzlj.sqjz.jz.cache.JzJbxxCache;
import com.hzlj.sqjz.jz.common.dto.common.JzJbxxCacheDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.hzlj.position.config.common.constant.Topic.POSITION_SKYNET_ADD;
import static com.hzlj.position.config.config.IdConfig.POSITION_ID_GEN;

/**
 * 定位记录-天网(PositionSkynet)表服务接口
 *
 * @author lifh
 * @date 2023-03-31 11:05:37
 */
@Slf4j
@Service
public class PositionSkynetServiceImpl implements PositionSkynetService {
    @Resource
    private PositionSkynetDao positionSkynetDao;
    @Resource
    private PositionAddTaskService positionAddTaskService;
    @Resource
    private JzJbxxCache jzJbxxCache;
    @Resource
    private KafkaTemplate<String, PositionSkynet> kafkaTemplate;

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PositionSkynetQueryResultDTO savePositionSkynet(PositionSkynetAddDTO dto) {
        PositionSkynet positionSkynet = PositionSkynetConverter.from(dto);
        positionSkynet.setId(POSITION_ID_GEN.nextIdStr());

        //默认
        positionSkynet.setWarningType(WarningType.SUCCESS.getCode());
        positionSkynet.setLeaving(false);

        //用户信息
        JzJbxxCacheDTO jzJbxx = jzJbxxCache.getJzJbxxByJzId(positionSkynet.getJzId());
        if (jzJbxx == null) {
            throw new PubException(String.format("用户不存在，请确认是否输在在矫人员:jzId=%s", positionSkynet.getJzId()));
        }
        positionSkynet.setJgCode(jzJbxx.getJgCode());
        positionSkynet.setJgMc(jzJbxx.getJgMc());
        positionSkynet.setXm(jzJbxx.getXm());

        if (ObjectUtil.isAllNotEmpty(dto.getLatitude(), dto.getLongitude())) {
            CrossAbnResultDTO abn = positionAddTaskService.isCrossWithMore(
                    positionSkynet.getJzId(),
                    positionSkynet.getJgCode(),
                    positionSkynet.getLongitude(),
                    positionSkynet.getLatitude()
            );
            //请假状态
            positionSkynet.setLeaving(abn.getLeaving());
            //越界
            if (abn.isCross()) {
                positionSkynet.setWarningType(WarningType.CROSS.getCode());
                //请假中
                if (Boolean.TRUE.equals(abn.getLeaving())) {
                    positionSkynet.setWarningType(WarningType.CROSS_LEAVING.getCode());
                }
            }
            //设置ext
            if (!abn.checkNull()) {
                positionSkynet.setPositionExt(BeanUtil.copyProperties(abn, PositionExtDTO.class));
            }
        }

        //添加队列
        kafkaTemplate.send(POSITION_SKYNET_ADD, positionSkynet.getId(), positionSkynet);
        return PositionSkynetConverter.from(positionSkynet);
    }

    @Override
    public void savePositionSkynet(List<PositionSkynet> positions) {
        positionSkynetDao.saveBatch(positions);
    }


    /**
     * 查询列表：不分页
     *
     * @param dto 查询条件
     * @return 对象列表
     */
    @Override
    @DictConvert
    public List<PositionSkynetQueryResultDTO> listPositionSkynet(PositionQueryParamDTO dto) {
        return this.positionSkynetDao.listPositionSkynet(dto);
    }

    /**
     * 查询列表：分页
     *
     * @param dto  查询条件
     * @param page 分页信息
     * @return 对象列表
     */
    @Override
    @DictConvert
    public QueryPageResultDTO<PositionSkynetQueryResultDTO> pagePositionSkynet(PositionQueryParamDTO dto, PageParamDTO page) {
        return this.positionSkynetDao.pagePositionSkynet(dto, page);
    }
}
